package com.sitanyun.merchant.guide.frament.view.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.AnalallAdapter;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.bean.AnalyDialsuserBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 经营分析详情
 */
public class AnalysDialsuserActivity extends BaseActivity {

    @BindView(R.id.scen_name)
    TextView scenName;
    @BindView(R.id.user_dials)
    RecyclerView userDials;
    @BindView(R.id.sw_untreated)
    SwipeRefreshLayout swUntreated;
    private String datetime ;
    private ArrayList<AnalyDialsuserBean.DataBean.ListBean> orderbeanlist;
    private AnalallAdapter analallAdapter;

    /**
     * 布局
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_analys_dialsuser;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("用户访问日志");
        //创建布局管理
        scenName.setText("客户：【" + getIntent().getStringExtra("cname") + "】的访问日志");
        orderbeanlist = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(AnalysDialsuserActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        userDials.setLayoutManager(layoutManager);
        //适配器
        analallAdapter = new AnalallAdapter(AnalysDialsuserActivity.this);
        userDials.setAdapter(analallAdapter);
        analallAdapter.notifyDataSetChanged();
        //请求数据
        initOkhttp(datetime);

    }

    // 数据请求
    private void initOkhttp(String date) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("date", date);
        hashMap.put("cId", getIntent().getStringExtra("cid"));
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.customerdetail).content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {
                swUntreated.setRefreshing(false);
            }

            @Override
            public void onResponse(String response, int id) {
                swUntreated.setRefreshing(false);
                AnalyDialsuserBean analyDialsuserBean = new Gson().fromJson(response, AnalyDialsuserBean.class);
              //添加到集合
                if (analyDialsuserBean.getCode() == 0) {
                    orderbeanlist.clear();
                    datetime = analyDialsuserBean.getData().getDate();
                    //无数据
                    if (analyDialsuserBean.getData().getList().size() == 0) {
                        analallAdapter.loadMoreEnd();
                        analallAdapter.notifyDataSetChanged();
                        return;
                    } else {
                        if (datetime==null){
                            analallAdapter.setNewData(analyDialsuserBean.getData().getList());
                            orderbeanlist.addAll(analyDialsuserBean.getData().getList());
                            analallAdapter.notifyDataSetChanged();
                        }else {
                            //orderbeanlist.addAll(orderBean.getData());
                            //底部没有数据时
                            if (analyDialsuserBean.getData().getList().size()==0) {
                                analallAdapter.loadMoreEnd();
                            } else {
                                //滑动加载数据
                                List<AnalyDialsuserBean.DataBean.ListBean> data1 =analyDialsuserBean.getData().getList();
                                orderbeanlist.addAll(data1);
                              //  datetime = dat;
                                analallAdapter.addData(data1);
                                analallAdapter.notifyDataSetChanged();
                                analallAdapter.loadMoreComplete();

                            }

                        }

                    }

                }

            }
        });

    }

    @Override
    public void initView() {
      //禁止下拉
        swUntreated.setRefreshing(false);
        swUntreated.setEnabled(false);
        //加载更多
        analallAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //请求数据
                initOkhttp(datetime);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
